using Microsoft.AspNetCore.Mvc;
using ERPBackend.Models;
using ERPBackend.Services;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ERPBackend.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class MaterialsController : ControllerBase
    {
        private readonly IMaterialService _materialService;

        public MaterialsController(IMaterialService materialService)
        {
            _materialService = materialService;
        }

        // GET: api/Materials
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Material>>> GetMaterials()
        {
            var materials = await _materialService.GetAllMaterialsAsync();
            return Ok(materials);
        }

        // GET: api/Materials/5
        [HttpGet("{id}")]
        public async Task<ActionResult<Material>> GetMaterial(int id)
        {
            var material = await _materialService.GetMaterialByIdAsync(id);

            if (material == null)
            {
                return NotFound();
            }

            return material;
        }

        // GET: api/Materials/BySecondLevelCategory/5
        [HttpGet("BySecondLevelCategory/{secondLevelCategoryId}")]
        public async Task<ActionResult<IEnumerable<Material>>> GetMaterialsBySecondLevelCategory(int secondLevelCategoryId)
        {
            var materials = await _materialService.GetMaterialsBySecondLevelCategoryIdAsync(secondLevelCategoryId);
            if (materials == null)
            {
                return NotFound("Error retrieving materials");
            }
            return Ok(materials);
        }

        // PUT: api/Materials/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutMaterial(int id, Material material)
        {
            if (id != material.Id)
            {
                return BadRequest();
            }

            try
            {
                var updated = await _materialService.UpdateMaterialAsync(material);
                if (!updated)
                {
                    return NotFound();
                }
                return NoContent();
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // POST: api/Materials
        [HttpPost]
        public async Task<ActionResult<Material>> PostMaterial(Material material)
        {
            try
            {
                var createdMaterial = await _materialService.CreateMaterialAsync(material);
                return CreatedAtAction("GetMaterial", new { id = createdMaterial.Id }, createdMaterial);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // DELETE: api/Materials/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteMaterial(int id)
        {
            var deleted = await _materialService.DeleteMaterialAsync(id);
            if (!deleted)
            {
                return NotFound();
            }
            return NoContent();
        }
    }
}